package com.leejxx.common.exception;

import com.leejxx.model.common.dto.ResponseResult;
import com.leejxx.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author LeeJx
 * @ClassName ExceptionCatch
 * @Description TODO
 * @date 2021/2/8 12:32
 * @Version 1.0
 */
@ControllerAdvice
@Slf4j
public class ExceptionCatch {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(Exception exception) {
        exception.printStackTrace();

        log.error("catch exception{}", exception.getMessage());

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult exception(CustomException customException) {
        customException.printStackTrace();

        log.error("catch exception{}", customException.getMessage());

        return customException.getResponseResult();
    }
}
